package com.su.controller;

import com.su.module.user;
import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class procontroller {
    @Autowired
    AmqpAdmin amqpAdmin;

    @Autowired
    RabbitTemplate rabbitTemplate;

    @GetMapping("/promqinit")
    public String promq(){
        DirectExchange directExchange = new DirectExchange("java-exchange",true,false);

        amqpAdmin.declareExchange(directExchange);

        System.out.println("交换机创建成功{}");

        Queue queue = new Queue("java-queue",true,false,false);
        amqpAdmin.declareQueue(queue);
        System.out.println("消息队列创建成功");

        Binding binding = new Binding("java-queue",
                Binding.DestinationType.QUEUE,"java-exchange",
                "java-queue",null);
        amqpAdmin.declareBinding(binding);
        System.out.println("绑定关系创建成功");

        return "消息队列初始化完成";
    }

    @GetMapping("/promqok")
    public String promqok(){
        user user = new user();

        user.setName("苏徐");
        user.setAge(23);
        user.setSex("男");

        for(int i=0;i<1;i++){
            rabbitTemplate.convertAndSend("java-exchange","java-queue",user);
            System.out.println("消息发送成功");
        }
        return "消息发送成功==>"+user.toString();
    }

}
